package com.mo.axion.handler.deduplication.service;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.mo.axion.common.domain.TaskInfo;
import com.mo.axion.common.enums.DeduplicationType;
import com.mo.axion.handler.deduplication.limit.LimitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author Mo
 * @createTime 2024/2/20 10:21
 * @description 内容去重服务（默认5分钟内相同的文案发送给同一个用户就会调用此服务）
 */
@Service
public class ContentDeduplicationService extends AbstractDeduplicationService{
    @Autowired
    public ContentDeduplicationService(@Qualifier("SlideWindowLimitService") LimitService limitService) {
        this.limitService = limitService;
        deduplicationType = DeduplicationType.CONTENT.getCode();
    }

    /**
     * 内容去重
     * 构建去重key
     *
     * @param taskInfo
     * @param receiver
     * @return
     */
    @Override
    public String deduplicationSingleKey(TaskInfo taskInfo, String receiver) {
        return DigestUtil.md5Hex(taskInfo.getMessageTemplateId() + receiver + JSON.toJSONString(taskInfo.getContentModel()));
    }
}
